package com.rt.gptnext.common.utils.http;

import cn.hutool.core.util.URLUtil;
import com.rt.gptnext.common.utils.SafeUtil;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author gaoyiyang
 **/
public class HttpUtil {

    public static HttpRequest request(String url) {
        return HttpRequest.create(url);
    }

    /**
     * 连接是否可以访问（状态码为2xx）
     *
     * @param url 请求地址
     * @return 是否可以访问
     */
    public static boolean urlIsOk(String url) {
        return SafeUtil.call(() -> {
            URL urlObj = URLUtil.url(url);
            HttpURLConnection connection = (HttpURLConnection) urlObj.openConnection();
            connection.setConnectTimeout(60000);
            connection.setReadTimeout(60000);
            int responseCode = connection.getResponseCode();
            if (responseCode >= 200 && responseCode < 300) {
                return true;
            }
            return false;
        }, false);

    }

}
